package com.scheduling.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scheduling.common.result.Result;
import com.scheduling.product.dto.ProductDTO;
import com.scheduling.product.dto.ProductQueryDTO;
import com.scheduling.product.entity.Product;
import com.scheduling.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 产品管理控制器
 *
 * @author 开发团队
 * @since 2025-05-29
 */
@Slf4j
@RestController
@RequestMapping("/api/product")
@Tag(name = "产品管理", description = "产品信息的增删改查操作")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;

    @Operation(summary = "分页查询产品列表", description = "根据条件分页查询产品信息")
    @PostMapping("/page")
    public Result<IPage<Product>> getProductPage(@RequestBody ProductQueryDTO queryDTO) {
        try {
            IPage<Product> page = productService.getProductPage(queryDTO);
            return Result.success(page);
        } catch (Exception e) {
            log.error("分页查询产品列表失败：{}", e.getMessage(), e);
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    @Operation(summary = "根据ID获取产品详情", description = "根据产品ID获取详细信息")
    @GetMapping("/{productId}")
    public Result<Product> getProductById(
            @Parameter(description = "产品ID", required = true) @PathVariable String productId) {
        try {
            Product product = productService.getProductById(productId);
            return Result.success(product);
        } catch (Exception e) {
            log.error("获取产品详情失败：{}", e.getMessage(), e);
            return Result.error("获取失败：" + e.getMessage());
        }
    }

    @Operation(summary = "添加产品", description = "添加新的产品信息")
    @PostMapping
    public Result<String> addProduct(@Validated @RequestBody ProductDTO productDTO) {
        try {
            boolean success = productService.addProduct(productDTO);
            if (success) {
                return Result.success("添加成功");
            } else {
                return Result.error("添加失败");
            }
        } catch (Exception e) {
            log.error("添加产品失败：{}", e.getMessage(), e);
            return Result.error("添加失败：" + e.getMessage());
        }
    }

    @Operation(summary = "更新产品", description = "更新产品信息")
    @PutMapping
    public Result<String> updateProduct(@Validated @RequestBody ProductDTO productDTO) {
        try {
            boolean success = productService.updateProduct(productDTO);
            if (success) {
                return Result.success("更新成功");
            } else {
                return Result.error("更新失败");
            }
        } catch (Exception e) {
            log.error("更新产品失败：{}", e.getMessage(), e);
            return Result.error("更新失败：" + e.getMessage());
        }
    }

    @Operation(summary = "删除产品", description = "根据ID删除产品")
    @DeleteMapping("/{productId}")
    public Result<String> deleteProduct(
            @Parameter(description = "产品ID", required = true) @PathVariable String productId) {
        try {
            boolean success = productService.deleteProduct(productId);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            log.error("删除产品失败：{}", e.getMessage(), e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    @Operation(summary = "批量删除产品", description = "根据ID列表批量删除产品")
    @DeleteMapping("/batch")
    public Result<String> deleteProducts(@RequestBody List<String> productIds) {
        try {
            boolean success = productService.deleteProducts(productIds);
            if (success) {
                return Result.success("批量删除成功");
            } else {
                return Result.error("批量删除失败");
            }
        } catch (Exception e) {
            log.error("批量删除产品失败：{}", e.getMessage(), e);
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }

    @Operation(summary = "获取所有产品列表", description = "获取所有产品的简要信息，用于下拉选择")
    @GetMapping("/all")
    public Result<List<Product>> getAllProducts() {
        try {
            List<Product> products = productService.getAllProducts();
            return Result.success(products);
        } catch (Exception e) {
            log.error("获取所有产品列表失败：{}", e.getMessage(), e);
            return Result.error("获取失败：" + e.getMessage());
        }
    }

    @Operation(summary = "检查产品名称是否存在", description = "检查产品名称是否已被使用")
    @GetMapping("/check-name")
    public Result<Boolean> checkProductNameExists(
            @Parameter(description = "产品名称", required = true) @RequestParam String productName,
            @Parameter(description = "排除的产品ID（更新时使用）") @RequestParam(required = false) String excludeId) {
        try {
            boolean exists = productService.checkProductNameExists(productName, excludeId);
            return Result.success(exists);
        } catch (Exception e) {
            log.error("检查产品名称失败：{}", e.getMessage(), e);
            return Result.error("检查失败：" + e.getMessage());
        }
    }
} 